package com.kaibes.admin.base;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kaibes.core.base.annotation.Description;
import com.kaibes.core.base.util.ClassUtils;
import com.kaibes.web.admin.IconBox;
import com.kaibes.web.api.ApiLink;
import com.kaibes.web.api.ParamValue;
import com.kaibes.web.permission.PermissionChecker;
import com.kaibes.web.util.ControllerUtils;

@com.kaibes.web.admin.AdminController
@RequestMapping("/admin")
public class AdminController {
    
    @Autowired
    private ObjectMapper objectMapper;
    @Resource
    private List<IconBox> iconBoxs;
    @Autowired
    private PermissionChecker permissionChecker;
    
//    @GetMapping("")
//    @ApiLink("后台首页")
//    public String home1(HttpServletRequest request, Model model) {
//        return ControllerUtils.forward("/admin/");
//    }
//    @GetMapping("/")
//    @ApiLink("后台首页")
//    public String home2(HttpServletRequest request, Model model) {
//        return "base";
//    }
    
    @GetMapping({"", "/"})
    @ApiLink("后台首页")
    public String home(HttpServletRequest request, Model model) {
        return ControllerUtils.forward("/admin/index");
    }
    
    @GetMapping("/index")
    @ApiLink("后台首页")
    public String index(@RequestAttribute Integer userId, Model model) {
    	List<IconBox> outBoxs = new ArrayList<>();
        iconBoxs.forEach(box->{
        	if (permissionChecker.hasPermissionGet(userId, box.getHref())) {
        		box.update();
        		outBoxs.add(box);
        	}
        });
        model.addAttribute("iconBoxs", outBoxs);
        return "index";
    }
    
    @PostMapping("/class")
    @ApiLink("类解析窗口")
    public String classStructure(Model model, @RequestParam @Description("完整类名") String name) {
        try {
            String type = null;
            if (name.contains("<")) {
                int i = name.indexOf("<");
                type = name.substring(i+1, name.length()-1);
                name = name.substring(0, i);
            }
            Class<?> clazz = Class.forName(name);
            if (type != null) {
                if (List.class.isAssignableFrom(clazz)) {
                    model.addAttribute("clazzRemark", "数组，"+ClassUtils.getDescription(type));
                } else {
                    model.addAttribute("clazzRemark", "未知泛型");
                }
                model.addAttribute("fieldDataList", ClassUtils.getListFieldData(Class.forName(type)));
            } else {
                if (clazz == String.class) {
                    model.addAttribute("clazzRemark", "字符串结果");
                } else {
                    model.addAttribute("clazzRemark", ClassUtils.getDescription(name));
                }
                model.addAttribute("fieldDataList", ClassUtils.getListFieldData(clazz));
            }
            if (clazz.isEnum()) {
                return "enum";
            } else {
                return "class";
            }
        } catch (ClassNotFoundException e) {
            return ControllerUtils.FORWARD_404;
        }
    }
    
    @PostMapping("/json")
    @ApiLink("json解析窗口")
    public String jsonStructure(Model model, @RequestParam @Description("参数JSON数组") String param) {
        try {
            param = URLDecoder.decode(param, "utf-8");
            List<ParamValue> params = objectMapper.readValue(param, new TypeReference<List<ParamValue>>() {
            });
            model.addAttribute("params", params);
        } catch (UnsupportedEncodingException | JsonProcessingException e) {
            model.addAttribute("params", new ArrayList<>());
        }
        return "json";
    }
    
}
